package com.haha.myapplication;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateMapThread implements Runnable{

    private static final String TAG = "RateThread";
    private Handler handler;
    public RateMapThread(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<HashMap<String,String>> relist = new ArrayList<HashMap<String,String>>();

        Log.i(TAG, "run: AAA....");
//      获取msg对象，用于返回主线程
//        Message msg = handler.obtainMessage();
//        msg.what = 6;
//        msg.obj = "Hello from run";
//        handler.sendMessage(msg);
//        Log.i(TAG, "run: 消息已发送");

        //获取网络数据
        URL url = null;
        try {
            //url = new URL("https://www.waihui321.com/eur-cny.html");
            //HttpURLConnection http = (HttpURLConnection) url.openConnection();
            //获得输入流,从输入流中捕获文本
            //InputStream in = http.getInputStream();
            //String html = inputStream2String(in);
            //Log.i(TAG, "run: html=" + html);

            Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
            Log.i(TAG, "run: title=" + doc.title());

//            Elements h4s = doc.getElementsByTag("h4");
//            for (Element h4 : h4s){
//                Log.i(TAG, "run: h4=" + h4.text());
//            }

            Elements tables = doc.getElementsByTag("table");
            Element table1 = tables.get(1);
            Log.i(TAG, "run: title=" + table1);

//            Elements hrefs = table1.getElementsByTag("a");
//            for (Element a : hrefs){
//                Log.i(TAG, "run: a=" + a.text());
//            }

            Elements trs = table1.getElementsByTag("tr");
            trs.remove(0);
            for (Element tr : trs){
                Elements tds = tr.getElementsByTag("td");
                String cname = tds.get(0).text();
                String cval = tds.get(5).text();
                Log.i(TAG, "run: cname=" + cname + " --> " + cval);
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("ItemTitle",cname);
                map.put("ItemDetail",cval);
                relist.add(map);
                //Log.i(TAG, "run: tr=" + tr);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

//      获取msg对象，用于返回主线程
        Message msg = handler.obtainMessage(9,relist);
        handler.sendMessage(msg);
        Log.i(TAG, "run: ");

    }
}
